<?php 
/**
* A Class able to get/set attributes both static and not.
*@package Atoms
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*/
class BaseClassGetSet
{
	private $cgs_id = false;
	private $cgs_storage = array();
	static private $cgs_static_storage = array();

	protected function InitializeStatic( $id )
	{
		$this->cgs_id = $id;
		BaseClassGetSet::$cgs_static_storage[$id] = array();
	}
	/**
	* Register allowed attributes and variables.
	*@param array $keys Named array with attributes and default initial values
	*/
	protected function SetStorage( $default_data )
	{
		foreach ( $default_data as $k => $v )
			$this->cgs_storage[$k] = $v;
	}
	/**
	* Register allowed attributes and variables.
	*@param array $keys Named array with attributes and default initial values
	*/
	protected function __construct( $default_data )
	{
		$this->SetStorage( $default_data );
	}
	/**
	*Return id
	*@return string
	*/
	public function GetId()
	{
		return $this->cgs_id;
	}
	/**
	* Return an attribute. Return false if attribute is not set.
	*@param string $attr The attribute to read
	*@return mixed
	*/
	protected function Get( $attr )
	{
		return isset( $this->cgs_storage[$attr] ) ? $this->cgs_storage[$attr] : false;
	}

	protected function GetStorage()
	{
		return $this->cgs_storage;
	}
	/**
	* Set an attribute. If not already registered, ignore it.
	*@param string $attr The attribute to set
	*@param mixed $value What to set to
	*@return mixed return false or $value is set successful
	*/
	protected function Set( $attr, $value )
	{
		if ( !isset( $this->cgs_storage[$attr] ) || !CoreAuth::Authorize() )
			return false;
		else if ( $this->cgs_storage[$attr] == $value )
			return false;
		else
			return $this->cgs_storage[$attr] = $value;
	}
	/**
	* Check an attribute.
	*@param string $attr The attribute to check
	*@return bool
	*/
	protected function Check( $attr )
	{
		return isset( $this->cgs_storage[$attr] ) ? $this->cgs_storage[$attr]!==false : false;
	}
	/**
	* Return a static attribute. Return false if attribute is not set.
	*@param string $attr The static attribute to read
	*@return mixed
	*/
	protected function GetStatic( $attr )
	{
		return isset( BaseClassGetSet::$cgs_static_storage[$this->cgs_id][$attr] ) ?
					BaseClassGetSet::$cgs_static_storage[$this->cgs_id][$attr] : false;
	}
	/**
	* Set a static attribute.
	* This will, of course, not set SetModified because a static data shall not be saved.
	*@param string $attr The static attribute to set
	*@param mixed $value What to set to
	*@return mixed return $value
	*/
	protected function SetStatic( $attr, $value )
	{
		return BaseClassGetSet::$cgs_static_storage[$this->cgs_id][$attr] = $value;
	}
}
?>